import logging
import unittest
import random

import requests

import app
from api.kaihu_api import Kaihu_api
from api.reg_login_api import Login_api
from api.tender_api import TenderApi
from utills import assert_untill, get_third_rquest_method, DB_Quid


class TestWorkflowapi(unittest.TestCase):
    loan_id = '862'
    def setUp(self) -> None:
        self.session=requests.Session()
        self.login_api=Login_api()
        self.kaihu=Kaihu_api()
        self.tenderapi=TenderApi()

    def tearDown(self) -> None:
        self.session.close()
    @classmethod
    def tearDownClass(cls) -> None:
        """最后运行的脚本之后删除测试数据"""
        del_sql_0 = "delete f.* from czbk_finance.fn_tender f inner join czbk_member.mb_member m on f.member_id = m.id where m.phone in ('12012345678','12012345679','12012345680','12012345681','12012345682')"
        del_sql_1 = "DELETE i.* from czbk_member.mb_member_info i INNER JOIN czbk_member.mb_member m ON i.member_id = m.id where m.phone in ('12012345678','12012345679','12012345680','12012345681','12012345682')"
        del_sql_2 = "DELETE l.* from czbk_member.mb_member_login_log l INNER JOIN czbk_member.mb_member m on l.member_id = m.id where m.phone in ('12012345678','12012345679','12012345680','12012345681','12012345682')"
        del_sql_3 = "delete from czbk_member.mb_member_register_log where phone in ('12012345678','12012345679','12012345680','12012345681','12012345682')"
        del_sql_4 = "DELETE from czbk_member.mb_member where phone in ('12012345678','12012345679','12012345680','12012345681','12012345682')"
        DB_Quid().db_quid(del_sql_0)
        DB_Quid().db_quid(del_sql_1)
        DB_Quid().db_quid(del_sql_2)
        DB_Quid().db_quid(del_sql_3)
        DB_Quid().db_quid(del_sql_4)

    def test01_workflow_api(self):

        #注册
        # 准备测试数据
        r = random.random()
        # 调用api类方法发送请求,接受响应
        self.login_api.get_img_code(self.session, r)
        # 对响应数据进行断言
        self.login_api.get_msg_code(self.session, app.phone5, app.imgcode)
        resp = self.login_api.register_api(self.session, app.phone5)
        assert_untill(self, resp, 200, 200, '注册成功')
        #登录
        resp = self.login_api.login_api(self.session, app.phone5)
        logging.info("resp={}".format(resp.json()))
        assert_untill(self, resp, 200, 200, '登录成功')
        #开户
        resp = self.kaihu.kaihuapi(self.session)
        logging.info("发送开户请求返回信息为:{}".format(resp.json()))
        self.assertEqual(200, resp.status_code)
        self.assertEqual(200, resp.json().get('status'))
        form_data = resp.json().get('description').get('form')
        res = get_third_rquest_method(self, form_data)
        logging.info("第三方开户请求返回数据:{}".format(res.text))
        self.assertEqual(200, res.status_code)
        self.assertIn('UserRegister OK', res.text)
        #充值
        r = random.random()
        res_2 = self.kaihu.get_recharge_code_api(self.session, r)
        self.assertEqual(200, res_2.status_code)
        logging.info("获取充值验证码返回状态码:{}".format(res_2.status_code))
        # 3获取充值信息
        amount = 1000
        res_3 = self.kaihu.get_recharge_msg_api(self.session, amount)
        logging.info("获取充值信息返回信息为:{}".format(res_3.json()))
        self.assertEqual(200, res_3.status_code)
        self.assertEqual(200, res_3.json().get('status'))
        # 4第三方充值
        form_data = res_3.json().get('description').get('form')
        res_4 = get_third_rquest_method(self, form_data)
        logging.info("第三方充值接口返回信息为:{}".format(res_4.text))
        self.assertEqual(200, res_4.status_code)
        self.assertEqual('NetSave OK', res_4.text)
        #查询投资产品详情
        res_2 = self.tenderapi.loan_info_api(self.session, TestWorkflowapi.loan_id)
        assert_untill(self, res_2, 200, 200, 'OK')
        logging.info("获取投资产品详情返回信息为:{}".format(res_2.json()))
        self.assertEqual(TestWorkflowapi.loan_id, res_2.json().get('data').get('loan_info').get('id'))
        #投资
        amount = 100
        res_2 = self.tenderapi.get_tender_api(self.session, TestWorkflowapi.loan_id, amount)
        logging.info("获取投资数据返回信息为:{}".format(res_2.json()))
        self.assertEqual(200, res_2.status_code)
        self.assertEqual(200, res_2.json().get('status'))
        # 第三方投资
        form_data = res_2.json().get('description').get('form')
        res_3 = get_third_rquest_method(self, form_data)
        self.assertEqual(200, res_3.status_code)
        self.assertEqual('InitiativeTender OK', res_3.text)
        logging.info("第三方投资接口返回数据为:{}".format(res_3.text))
        #获取我的投资列表
        tender = 'tender'
        res_2 = self.tenderapi.get_mylist_api(self.session, tender)
        self.assertEqual(200, res_2.status_code)
        logging.info("获取我的投资列表返回的信息为:{}".format(res_2.json()))
        query_sql = "SELECT count(*) from czbk_finance.fn_tender f INNER JOIN czbk_member.mb_member m on f.member_id=m.id where m.phone='{}' and f.`status`='-2';"
        result = DB_Quid().db_quid(query_sql.format(app.phone5))
        logging.info("数据库查询到的条数为:{}".format(result[0][0]))
        self.assertEqual(result[0][0], res_2.json().get('total_items'))
